/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.itriage.hospital.client.web.controllers;

import com.itriage.hospital.client.web.web_model.StaffModel;
import com.itriage.hospital.model.Staff;
import com.itriage.hospital.services.StaffService;
import com.itriage.hospital.services.crud.StaffCrudServices;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

/**
 *
 * @author Yandiswa
 */
@Controller
@SessionAttributes
public class StaffController {
    @Autowired
    private StaffService staffService;
    
    private static ApplicationContext ctx;
    private StaffCrudServices staffCrudServices;
    
    @RequestMapping(value = "/staffform", method = RequestMethod.GET)
    public String getStaffForm(Model model) {
        StaffModel staffModel = new StaffModel();
        model.addAttribute("staffModel", staffModel);
        return "staff/home";
    }//@Valid
    
    
    @RequestMapping(value = "/createstaff", method = RequestMethod.POST)
    public String createStaffs(@ModelAttribute("staffModel")  StaffModel staff,
            BindingResult result, Model model) {
        
        ctx = new ClassPathXmlApplicationContext("classpath:com/itriage/hospital/app/conf/applicationContext-*.xml");
        staffCrudServices = (StaffCrudServices)ctx.getBean("staffCrudServices");
        
        Staff s = new Staff(); // DONT DO THIS USE FACTORY
        s.setStaffNum(staff.getStaffNum());
        s   .setEducation(staff.getEducation());
        staffCrudServices.persist(s);
        //staffService.createStaff(staff);

        List<Staff> staffs = staffService.getStaff();
        model.addAttribute("staffs", staffs);
        return "staff/staffs";
    }
    
    
     @RequestMapping(value = "/staffeditform", method = RequestMethod.GET)
    public String getStaffEditForm(Model model) {
        StaffModel staffModel = new StaffModel();
        model.addAttribute("staff", staffModel);
        return "staff/home";
    }
     
     
     @RequestMapping(value = "/staffs", method = RequestMethod.GET)
    public String getStaffs(Model model) {
        List<Staff> staffs = staffService.getStaff();
        model.addAttribute("staffs", staffs);
        return "staff/staffs";
    }
    
     @RequestMapping(value = "/editstaff", method = RequestMethod.GET)
    public String editStaffs(Model model) {
        List<Staff> staffs = staffService.getStaff();
        model.addAttribute("staffs", staffs);
        return "staff/staffs";
    }

    @RequestMapping(value = "/deletestaff", method = RequestMethod.GET,params = {"staffNumId"})
    public String deleteStaffs(@RequestParam("staffNumId") Long staffNumId,Model model) {
        staffService.deleteStaff(staffNumId);
        List<Staff> staffs = staffService.getStaff();
        model.addAttribute("staffs", staffs);
        return "staff/staffs";
    }

    @RequestMapping(value = "private/editStaffForm.html", method = RequestMethod.GET, params = {"staffNumId"})
    public String editCourseForm(@RequestParam("staffNumId") String staffNumId, Model model) {

        return "editIndividualStafForm";
    }
     
}
